const query=require('../db/mysql')
const {success,error}=require('../model/model')
const {md5}=require('utility')
const jwt=require('jsonwebtoken')

class ListController{
    ///注册
   register=async(ctx)=>{
       const {username,password,nickname}=ctx.request.body;
      const isHaveSql=`select * from users where username="${username}" `
      const isHave=await query(isHaveSql)
      if(isHave.length){
          ctx.body=error('账号已存在')
          return
      }else{
          const sql=`insert into users(username,password,nickname) values("${username}","${md5(String(password))}","${nickname}") `;
          const data=await query(sql)
         //ctx.body=data
        if (data.affectedRows === 1) {
            ctx.body = success('注册成功')
        } else {
            ctx.body = error('注册失败')
        }
      }
   }

   ///登录
   login=async(ctx)=>{
       const {username}=ctx.request.body;
       const sql=`select * from users where username="${username}" `;
       const data=await query(sql)
       if(data.length){
           ctx.body=success("登录成功")
       }else{
           ctx.body=error('登录失败')
       }
   }
}

module.exports=new ListController()